// backManageDlg.cpp : implementation file
//

#include "stdafx.h"
#include "msxml.h"
#include "backManage.h"
#include "backManageDlg.h"
#include "TCPClientSocket.h"
#include "UDPSocket.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	enum { IDD = IDD_ABOUTBOX };

	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support

// Implementation
protected:
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
END_MESSAGE_MAP()


// CbackManageDlg dialog




CbackManageDlg::CbackManageDlg(CWnd* pParent /*=NULL*/)
	: CMaxDisplayDlg(CbackManageDlg::IDD, pParent)
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CbackManageDlg::DoDataExchange(CDataExchange* pDX)
{
	CMaxDisplayDlg::DoDataExchange(pDX);
	//DDX_Control(pDX, IDC_TAB1, m_TabSheet);
}

BEGIN_MESSAGE_MAP(CbackManageDlg, CMaxDisplayDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
	ON_WM_CLOSE()
	ON_BN_CLICKED(IDC_BTN_MENU, &CbackManageDlg::OnBnClickedBtnMenu)
	ON_BN_CLICKED(IDC_BTN_EMPLOYEE, &CbackManageDlg::OnBnClickedBtnEmployee)
	ON_BN_CLICKED(IDC_BTN_CUSTOMER, &CbackManageDlg::OnBnClickedBtnCustomer)
	ON_BN_CLICKED(IDC_BTN_PERMISSION, &CbackManageDlg::OnBnClickedBtnPermission)
	ON_BN_CLICKED(IDC_BTN_COST, &CbackManageDlg::OnBnClickedBtnCost)
	ON_BN_CLICKED(IDC_BTN_STATISTICS, &CbackManageDlg::OnBnClickedBtnStatistics)
	ON_BN_CLICKED(IDC_BTN_SETTING, &CbackManageDlg::OnBnClickedBtnSetting)
	ON_MESSAGE(RCV_MSG+MAIN_DLG_ID, &CbackManageDlg::OnNetMessage)	
END_MESSAGE_MAP()


// CbackManageDlg message handlers

BOOL CbackManageDlg::OnInitDialog()
{
	CMaxDisplayDlg::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon

	// TODO: Add extra initialization here
	BOOL ret;
	::OleInitialize(NULL);
	
	HRESULT hr = m_plDomDocument.CreateInstance(MSXML::CLSID_DOMDocument);
	if(FAILED(hr))
	{
		_com_error er(hr);
		AfxMessageBox(er.ErrorMessage());
		EndDialog(1);
	}

	MSGWnd msgwnd;
	msgwnd.hwnd=GetSafeHwnd();
	msgwnd.index=0;
	RegisterNetMsg(&msgwnd, MAIN_DLG_ID);
	m_hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
	m_nMenuSerVer=0;
	GetBMConfigXML(&m_SettingTableDlg.m_nServerIP, &m_SettingTableDlg.m_nServerPort, 
					&m_SettingTableDlg.m_nUDPPort, &m_nMenuVer, &m_nConfVer
					);

	m_pTCPClientSocket = new CTCPClientSocket(this);
	m_pUDPSocket = new CUDPSocket(this);

	ret=TCPServerInit(m_pTCPClientSocket, m_SettingTableDlg.m_nServerIP, m_SettingTableDlg.m_nServerPort);
	m_SettingTableDlg.m_nNetStatus=ret?"Connnected OK":"Not Connnected";
	UDPServerInit(m_pUDPSocket, m_SettingTableDlg.m_nUDPPort);
	if(ret)
	{
		TxGetXMLVersion();
	}

	if(m_nMenuSerVer>m_nMenuVer)
	{
		TxGetMenuXML();
	}
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CbackManageDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CMaxDisplayDlg::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CbackManageDlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CMaxDisplayDlg::OnPaint();
	}
}

// The system calls this function to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CbackManageDlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}


void CbackManageDlg::OnClose()
{
	// TODO: Add your message handler code here and/or call default

	delete m_pTCPClientSocket;
	delete m_pUDPSocket;
	CMaxDisplayDlg::OnClose();
}



LRESULT CbackManageDlg::OnNetMessage(WPARAM wParam, LPARAM lParam)
{
	u8 *buf=(u8 *) wParam;
	u32 n=(u32) lParam;

	u32 cmd;
	memcpy(&cmd, buf, sizeof(cmd));

	switch(cmd)
	{
		case CMD_RGETXMLVER:
			RxGetXMLVersion((XMLVersion *)(buf+sizeof(cmd)));
			break;
		case CMD_RGETXMLMENU:
			RxGetMenuXML((CMDRes *)(buf+sizeof(cmd)));
			break;
		default:
			break;
	}


	return 0;
}

void CbackManageDlg::OnBnClickedBtnMenu()
{
	// TODO: Add your control notification handler code here
	CMenuTableDlg		MenuTableDlg;
	if( MenuTableDlg.DoModal() == IDOK )
	{
	}

}

void CbackManageDlg::OnBnClickedBtnEmployee()
{
	// TODO: Add your control notification handler code here
	CEmployeeTableDlg		EmployeeTableDlg;
	if( EmployeeTableDlg.DoModal() == IDOK )
	{
	}

}

void CbackManageDlg::OnBnClickedBtnCustomer()
{
	// TODO: Add your control notification handler code here
}

void CbackManageDlg::OnBnClickedBtnPermission()
{
	// TODO: Add your control notification handler code here
}

void CbackManageDlg::OnBnClickedBtnCost()
{
	// TODO: Add your control notification handler code here
}

void CbackManageDlg::OnBnClickedBtnStatistics()
{
	// TODO: Add your control notification handler code here
}

void CbackManageDlg::OnBnClickedBtnSetting()
{
	// TODO: Add your control notification handler code here
	CSettingTableDlg		SettingDlg;
	SettingDlg.m_nServerIP=m_SettingTableDlg.m_nServerIP;
	SettingDlg.m_nServerPort=m_SettingTableDlg.m_nServerPort;
	SettingDlg.m_nUDPPort=m_SettingTableDlg.m_nUDPPort;
	SettingDlg.m_nNetStatus=m_SettingTableDlg.m_nNetStatus;
	if( SettingDlg.DoModal() == IDOK )
	{
		m_SettingTableDlg.m_nServerIP=SettingDlg.m_nServerIP;
		m_SettingTableDlg.m_nServerPort=SettingDlg.m_nServerPort;
		m_SettingTableDlg.m_nUDPPort=SettingDlg.m_nUDPPort;
		m_SettingTableDlg.m_nNetStatus=SettingDlg.m_nNetStatus;
	}
}

void CbackManageDlg::TxGetXMLVersion(void)
{
	DWORD ret;
	u8 buf[2048];

	m_nTxCMD=CMD_TGETXMLVER;
	memcpy(buf, &m_nTxCMD, sizeof(m_nTxCMD));	
	TCPSendPacket(buf, sizeof(m_nTxCMD));

	
	ret=WaitForSingleObject(m_hEvent, TMO_REDITMENU); 
	if(ret==WAIT_TIMEOUT)
	{
		AfxMessageBox(_T("OnBnClickedBtnMenuAdd FAILED"));
	}

}

void CbackManageDlg::TxGetMenuXML(void)
{
	DWORD ret;
	u8 buf[2048];

	m_nTxCMD=CMD_TGETXMLMENU;
	memcpy(buf, &m_nTxCMD, sizeof(m_nTxCMD));	
	TCPSendPacket(buf, sizeof(m_nTxCMD));

	
	ret=WaitForSingleObject(m_hEvent, TMO_REDITMENU); 
	if(ret==WAIT_TIMEOUT)
	{
		AfxMessageBox(_T("OnBnClickedBtnMenuAdd FAILED"));
	}
	else
	{
		TCPReceiveFile(_T("MenuList.xml"));
	}
}

void CbackManageDlg::RxGetXMLVersion(XMLVersion *pVer)
{
	if(m_nTxCMD!=CMD_TGETXMLVER)
	{
		m_nRxErr=1;
	}
	else
	{
		m_nMenuSerVer=pVer->m_nMenuVer;
	}

	SetEvent(m_hEvent);
}

void CbackManageDlg::RxGetMenuXML(CMDRes *pRet)
{
	if(m_nTxCMD!=CMD_TGETXMLMENU)
	{
		m_nRxErr=1;
	}
	else
	{
		m_nMenuSerVer=pRet->m_nRet;
	}

	SetEvent(m_hEvent);
}

